package cn.zjyj.module.zjyj.controller.admin.riskidentifyinstance;

import cn.zjyj.module.zjyj.controller.admin.deviceinstance.vo.DeviceInstanceRespVO;
import cn.zjyj.module.zjyj.dal.dataobject.deviceinstance.DeviceInstanceDO;
import cn.zjyj.module.zjyj.dal.mysql.device.DeviceMapper;
import cn.zjyj.module.zjyj.dal.mysql.deviceinstance.DeviceInstanceMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.zjyj.framework.common.common.pojo.PageParam;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.pojo.CommonResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;
import static cn.zjyj.framework.common.common.pojo.CommonResult.success;

import cn.zjyj.framework.excel.core.util.ExcelUtils;

import cn.zjyj.framework.apilog.core.annotation.ApiAccessLog;
import static cn.zjyj.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.zjyj.module.zjyj.controller.admin.riskidentifyinstance.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.riskidentifyinstance.RiskIdentifyInstanceDO;
import cn.zjyj.module.zjyj.service.riskidentifyinstance.RiskIdentifyInstanceService;

@Tag(name = "管理后台 - 风险辨识实例")
@RestController
@RequestMapping("/zjyj/risk-identify-instance")
@Validated
public class RiskIdentifyInstanceController {

    @Resource
    private RiskIdentifyInstanceService riskIdentifyInstanceService;

    @Resource
    DeviceMapper deviceMapper;

    @Resource
    DeviceInstanceMapper deviceInstanceMapper;

    @PostMapping("/create")
    @Operation(summary = "创建风险辨识实例")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-instance:create')")
    public CommonResult<Long> createRiskIdentifyInstance(@Valid @RequestBody RiskIdentifyInstanceSaveReqVO createReqVO) {
        return success(riskIdentifyInstanceService.createRiskIdentifyInstance(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新风险辨识实例")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-instance:update')")
    public CommonResult<Boolean> updateRiskIdentifyInstance(@Valid @RequestBody RiskIdentifyInstanceSaveReqVO updateReqVO) {
        riskIdentifyInstanceService.updateRiskIdentifyInstance(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除风险辨识实例")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-instance:delete')")
    public CommonResult<Boolean> deleteRiskIdentifyInstance(@RequestParam("id") Long id) {
        riskIdentifyInstanceService.deleteRiskIdentifyInstance(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除风险辨识实例")
                @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-instance:delete')")
    public CommonResult<Boolean> deleteRiskIdentifyInstanceList(@RequestParam("ids") List<Long> ids) {
        riskIdentifyInstanceService.deleteRiskIdentifyInstanceListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得风险辨识实例")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-instance:query')")
    public CommonResult<RiskIdentifyInstanceRespVO> getRiskIdentifyInstance(@RequestParam("id") Long id) {
        RiskIdentifyInstanceDO riskIdentifyInstance = riskIdentifyInstanceService.getRiskIdentifyInstance(id);
        return success(BeanUtils.toBean(riskIdentifyInstance, RiskIdentifyInstanceRespVO.class));
    }
    @GetMapping("/getDeviceInstance")
    @Operation(summary = "获得风险辨识实例")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-instance:query')")
    public CommonResult<DeviceInstanceRespVO> getDeviceInstance(@RequestParam("identifyInstanceId") Long id) {
        RiskIdentifyInstanceDO riskIdentifyInstance = riskIdentifyInstanceService.getRiskIdentifyInstance(id);
        DeviceInstanceDO deviceInstanceDO = deviceInstanceMapper.selectById(riskIdentifyInstance.getInstanceId());
        DeviceInstanceRespVO bean = BeanUtils.toBean(deviceInstanceDO, DeviceInstanceRespVO.class);
        bean.setDeviceName(deviceMapper.selectById(bean.getDeviceId()).getName());
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得风险辨识实例分页")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-instance:query')")
    public CommonResult<PageResult<RiskIdentifyInstanceRespVO>> getRiskIdentifyInstancePage(@Valid RiskIdentifyInstancePageReqVO pageReqVO) {
        PageResult<RiskIdentifyInstanceDO> pageResult = riskIdentifyInstanceService.getRiskIdentifyInstancePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RiskIdentifyInstanceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出风险辨识实例 Excel")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-identify-instance:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRiskIdentifyInstanceExcel(@Valid RiskIdentifyInstancePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RiskIdentifyInstanceDO> list = riskIdentifyInstanceService.getRiskIdentifyInstancePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "风险辨识实例.xls", "数据", RiskIdentifyInstanceRespVO.class,
                        BeanUtils.toBean(list, RiskIdentifyInstanceRespVO.class));
    }

}